﻿<%@ WebHandler Language="C#" Class="list" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using System.Web.Services;
using System.IO;
using System.Drawing;


public class list : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private StringBuilder sb = null;
    public DataSet ds = null;
    public DataTable dt = null;
    private string sqlwhere = "";
    public void ProcessRequest (HttpContext context) {
        string type = PageBase.ext_string(context.Request.Params["type"]);
        switch (type)
        {
            //查询类型下拉选项
            case "getselect":
                getselect(context);
                break;
            //获取返修信息列表
            case "getlist":
                getinfo_list(context);
                break;
            //获取订单审核列表
            case "getshlist":
                getinfo_shlist(context);
                break;
           //根据量订订单获取订单列表
            case "getlist_bylddh":
                getlist_bylddh(context);
                break;
            //新增
            case "add":
                add(context);
                break;
            //修改
            case "edit":
                edit(context);
                break;
            //删除
            case "delete":
                delete(context);
                break;
            //根据id获取信息
            case "getlist_byid":
                getlist_byid(context);
                break;
            //根据量订订单id获取信息
            case "getlist_byorderid":
                getlist_byorderid(context);
                break;
            //审核
            case "audit":
                audit(context);
                break;
            //驳回
            case "return_order":
                return_order(context);
                break;
            //获取操作流水记录
            case "getlist_ls":
                getinfo_list_ls(context);
                break;
            default:
                //getinfo(context);
                break;
        }
    }
    //获取操作流水
    public void getinfo_list_ls(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);

        string djdocno = PageBase.ext_string(context.Request.Params["djdocno"]);
        string orderby = "cztime desc";
        string where = @"select  COUNT(*) OVER(PARTITION BY 1) AS TOTAL,rebh,cz,czrname,cztime,remark
                        from t_rework_czlog where 1=1 and rebh='"+djdocno+"'";

        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());

        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"rebh\":\"" + dt.Rows[i]["rebh"].ToString() + "\"");
            sb.Append(",\"cz\":\"" + dt.Rows[i]["cz"] + "\"");
            sb.Append(",\"czrname\":\"" + dt.Rows[i]["czrname"] + "\"");
            sb.Append(",\"cztime\":\"" + dt.Rows[i]["cztime"] + "\"");
            sb.Append(",\"remark\":\"" + dt.Rows[i]["remark"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //驳回
    public void return_order(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string userid = PageBase.ext_string(context.Request.Params["userid"]);
        string username = PageBase.ext_string(context.Request.Params["usernames"]);
        string docno = PageBase.ext_string(context.Request.Params["docno"]);

        string return_remark = PageBase.ext_string(context.Request.Params["return_remark"]);
        string msg = "";
        bool flag;
        string sqldelete = "update t_rework_main set status='-1',audit_remark='{0}' where id in ({1})";
        sqldelete = string.Format(sqldelete,return_remark, getids);
        int dtcount= DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {
            flag = true;
            msg = "驳回成功";

            string sql_zt = @"insert into t_rework_czlog(rebh,cz,czrid,czrname,cztime,remark) values('{0}','{1}','{2}','{3}','{4}','{5}')";
            sql_zt = string.Format(sql_zt,docno,"驳回",userid,username,DateTime.Now.ToString(),return_remark);
            DBHelper.ExecuteNonQuery(sql_zt);
        }
        else
        {
            flag = false;
            msg = "驳回失败";

        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //审核
    public void audit(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string userid = PageBase.ext_string(context.Request.Params["userid"]);
        string username = PageBase.ext_string(context.Request.Params["usernames"]);
        string docno = PageBase.ext_string(context.Request.Params["docno"]);

        string msg = "";
        bool flag=false;

        string sqldelete = @"update t_rework_main set status='99',modifier='{0}',updatetime='{1}' where id in ({2});";
        sqldelete = string.Format(sqldelete, userid,DateTime.Now.ToString(),getids);
        int dtcount = DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {

            flag = true;
            msg = "审核成功，伯俊处理成功";

            string sql_zt = @"insert into t_rework_czlog(rebh,cz,czrid,czrname,cztime,remark) values('{0}','{1}','{2}','{3}','{4}','{5}')";
            sql_zt = string.Format(sql_zt, docno, "审核", userid, username, DateTime.Now.ToString(), "");
            DBHelper.ExecuteNonQuery(sql_zt);
        }
        else
        {
            flag = false;
            msg = "审核失败";
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //根据量订订单id查询
    public void getlist_byorderid(HttpContext context)
    {
        string id = PageBase.ext_string(context.Request.Params["ids"]);
        string sql = @"  select id,docno,ld_docno,product_name,product_value,product_count from t_order_main  where id={0}";
        sql = string.Format(sql, id);
        DataTable dt = DBHelper.ExecuteDataTable(sql);
        sb = new StringBuilder();
        string code = "0";
        string msg = "";

        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            //string urlgsh = "../../" + dt.Rows[i]["imgurl"];
            sb.Append("{\"id\":\"" + Utility.Trim(dt.Rows[i]["id"].ToString()) + "\"");
            sb.Append(",\"docno\":\"" + Utility.Trim(dt.Rows[i]["docno"]) + "\"");
            sb.Append(",\"ld_docno\":\"" + Utility.Trim(dt.Rows[i]["ld_docno"]) + "\"");
            sb.Append(",\"product_name\":\"" + Utility.Trim(dt.Rows[i]["product_name"]) + "\"");
            sb.Append(",\"product_value\":\"" + Utility.Trim(dt.Rows[i]["product_value"]) + "\"");
            sb.Append(",\"product_count\":\"" + Utility.Trim(dt.Rows[i]["product_count"]) + "\"}");

            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
       
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询图标根据id
    public void getlist_byid(HttpContext context)
    {
        string id = PageBase.ext_string(context.Request.Params["editid"]);
        string sql = @" select id,rebh,ysdh,orderbh,kh,name,sl,remark,status from [dbo].t_rework_main  where id={0}";
        sql = string.Format(sql,id);
        DataTable dt = DBHelper.ExecuteDataTable(sql);
        sb = new StringBuilder();
        string code = "0";
        string msg = "";

        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            //string urlgsh = "../../" + dt.Rows[i]["imgurl"];
            sb.Append("{\"id\":\"" + Utility.Trim(dt.Rows[i]["id"].ToString()) + "\"");
            sb.Append(",\"rebh\":\"" + Utility.Trim(dt.Rows[i]["rebh"]) + "\"");
            sb.Append(",\"ysdh\":\"" + Utility.Trim(dt.Rows[i]["ysdh"]) + "\"");
            sb.Append(",\"orderbh\":\"" + Utility.Trim(dt.Rows[i]["orderbh"]) + "\"");
            sb.Append(",\"kh\":\"" + Utility.Trim(dt.Rows[i]["kh"]) + "\"");
            sb.Append(",\"name\":\"" + Utility.Trim(dt.Rows[i]["name"]) + "\"");
            sb.Append(",\"sl\":\"" +Utility.Trim(dt.Rows[i]["sl"]) + "\"");
            sb.Append(",\"remark\":\"" + Utility.Trim(dt.Rows[i]["remark"]) + "\"}");

            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("],");
        //备注图片信息
        string sql_bzpic = @"select id,rebh,picurl from t_rework_img where rebh='{0}'";
        sql_bzpic = string.Format(sql_bzpic,dt.Rows[0]["rebh"]);
        DataTable dt_pic = DBHelper.ExecuteDataTable(sql_bzpic);
        sb.Append("\"data_pic\":[");
        for (int x = 0; x < dt_pic.Rows.Count; x++)
        {
            string urlgsh = "../"+dt_pic.Rows[x]["picurl"];
            sb.Append("{\"id\":\"" + dt_pic.Rows[x]["id"].ToString() + "\",");
            sb.Append("\"rebh\":\"" + dt_pic.Rows[x]["rebh"].ToString() + "\",");
            sb.Append("\"picurl\":\"" + urlgsh + "\"}");
            if (x < dt_pic.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //删除
    public void delete(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sqldelete = "update  t_rework_main set isactive='N' where id in ({0})";
        sqldelete = string.Format(sqldelete, getids);
        int dtcount= DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {
            flag = true;
            msg = "删除成功";
        }
        else
        {
            flag = false;
            msg = "删除失败";

        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询审核列表
    public void getinfo_shlist(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);

        string orderby = "createtime";
        string where = @"select  COUNT(*) OVER(PARTITION BY 1) AS TOTAL,a.id,a.rebh,a.ysdh,a.orderbh,a.kh,a.name,
					    a.sl,a.remark,a.status,b.username,a.createtime,c.username updater,a.updatetime,ltrim(rtrim(a.isactive)) isactive  from 
                       [dbo].t_rework_main a left join sys_users b on a.creater=b.id 
					   left join sys_users c on a.modifier=c.id where ltrim(rtrim(a.isactive))='Y'
                       and 1=1  ";
        string txt_ysdh =PageBase.ext_string(context.Request.Params["txt_ysdh"]);
        string txt_orderdh =PageBase.ext_string(context.Request.Params["txt_orderdh"]);
        string status =PageBase.ext_string(context.Request.Params["status"]);
        string txt_kh =PageBase.ext_string(context.Request.Params["txt_kh"]);
        //预收单号
        if (!string.IsNullOrEmpty(txt_ysdh))
        {
            where += " and a.ysdh like N'%" + txt_ysdh + "%'";
        }
        //量定单号
        if (!string.IsNullOrEmpty(txt_orderdh))
        {
            where += " and a.orderbh like N'%" + txt_orderdh + "%'";
        }
        //状态
        if (!string.IsNullOrEmpty(status))
        {
            where += " and a.status=" + status + "";
        }
        //款号
        if (!string.IsNullOrEmpty(txt_kh))
        {
            where += " and a.kh like N'%" + txt_kh + "%'";
        }
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());
        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"rebh\":\"" + dt.Rows[i]["rebh"] + "\"");
            sb.Append(",\"ysdh\":\"" + dt.Rows[i]["ysdh"] + "\"");
            sb.Append(",\"orderbh\":\"" + dt.Rows[i]["orderbh"] + "\"");
            sb.Append(",\"kh\":\"" + dt.Rows[i]["kh"] + "\"");
            sb.Append(",\"name\":\"" + dt.Rows[i]["name"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"remark\":\"" + dt.Rows[i]["remark"] + "\"");
            sb.Append(",\"status\":\"" + dt.Rows[i]["status"] + "\"");
            sb.Append(",\"username\":\"" + dt.Rows[i]["username"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"");
            sb.Append(",\"updater\":\"" + dt.Rows[i]["updater"] + "\"");
            sb.Append(",\"updatetime\":\"" + dt.Rows[i]["updatetime"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询
    public void getinfo_list(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);

        string orderby = "createtime";
        string where = @"select  COUNT(*) OVER(PARTITION BY 1) AS TOTAL,a.id,a.rebh,a.ysdh,a.orderbh,a.kh,a.name,
					    a.sl,a.remark,a.status,b.username,a.createtime,c.username updater,a.updatetime,ltrim(rtrim(a.isactive)) isactive  from 
                       [dbo].t_rework_main a left join sys_users b on a.creater=b.id 
					   left join sys_users c on a.modifier=c.id where ltrim(rtrim(a.isactive))='Y'
                       and 1=1  ";
        string txt_ysdh =PageBase.ext_string(context.Request.Params["txt_ysdh"]);
        string txt_orderdh =PageBase.ext_string(context.Request.Params["txt_orderdh"]);
        string status =PageBase.ext_string(context.Request.Params["status"]);
        string txt_kh =PageBase.ext_string(context.Request.Params["txt_kh"]);
        //预收单号
        if (!string.IsNullOrEmpty(txt_ysdh))
        {
            where += " and a.ysdh like N'%" + txt_ysdh + "%'";
        }
        //量定单号
        if (!string.IsNullOrEmpty(txt_orderdh))
        {
            where += " and a.orderbh like N'%" + txt_orderdh + "%'";
        }
        //状态
        if (!string.IsNullOrEmpty(status))
        {
            where += " and a.status=" + status + "";
        }
        //款号
        if (!string.IsNullOrEmpty(txt_kh))
        {
            where += " and a.kh like N'%" + txt_kh + "%'";
        }
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());
        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"rebh\":\"" + dt.Rows[i]["rebh"] + "\"");
            sb.Append(",\"ysdh\":\"" + dt.Rows[i]["ysdh"] + "\"");
            sb.Append(",\"orderbh\":\"" + dt.Rows[i]["orderbh"] + "\"");
            sb.Append(",\"kh\":\"" + dt.Rows[i]["kh"] + "\"");
            sb.Append(",\"name\":\"" + dt.Rows[i]["name"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"remark\":\"" + dt.Rows[i]["remark"].ToString() + "\"");
            sb.Append(",\"status\":\"" + dt.Rows[i]["status"] + "\"");
            sb.Append(",\"username\":\"" + dt.Rows[i]["username"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"");
            sb.Append(",\"updater\":\"" + dt.Rows[i]["updater"] + "\"");
            sb.Append(",\"updatetime\":\"" + dt.Rows[i]["updatetime"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
         //根据量订单号查询
    public void getlist_bylddh(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);

        string orderby = "createtime";
        string where = @"select  COUNT(*) OVER(PARTITION BY 1) AS TOTAL,a.id,a.rebh,a.ysdh,a.orderbh,a.kh,a.name,
					    a.sl,a.remark,a.status,b.username,a.createtime,c.username updater,a.updatetime,ltrim(rtrim(a.isactive)) isactive  from 
                       [dbo].t_rework_main a left join sys_users b on a.creater=b.id 
					   left join sys_users c on a.modifier=c.id where ltrim(rtrim(a.isactive))='Y'
                       and 1=1  ";
        string txt_ysdh =PageBase.ext_string(context.Request.Params["ysddh"]);
       
        //预收单号
        if (!string.IsNullOrEmpty(txt_ysdh))
        {
            where += " and a.ysdh = '" + txt_ysdh + "'";
        }
       
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());
        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"rebh\":\"" + dt.Rows[i]["rebh"] + "\"");
            sb.Append(",\"ysdh\":\"" + dt.Rows[i]["ysdh"] + "\"");
            sb.Append(",\"orderbh\":\"" + dt.Rows[i]["orderbh"] + "\"");
            sb.Append(",\"kh\":\"" + dt.Rows[i]["kh"] + "\"");
            sb.Append(",\"name\":\"" + dt.Rows[i]["name"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"remark\":\"" + dt.Rows[i]["remark"] + "\"");
            sb.Append(",\"status\":\"" + dt.Rows[i]["status"] + "\"");
            sb.Append(",\"username\":\"" + dt.Rows[i]["username"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"");
            sb.Append(",\"updater\":\"" + dt.Rows[i]["updater"] + "\"");
            sb.Append(",\"updatetime\":\"" + dt.Rows[i]["updatetime"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询类型下拉选项
    public void getselect(HttpContext context)
    {
        string ysdid = PageBase.ext_string(context.Request.Params["ysdid"]);
        string sql = @"select * from [t_type] where isactive='Y'";
        DataTable dt = DBHelper.ExecuteDataTable(sql);

        string fsmessage = "";
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"typename\":\"" + dt.Rows[i]["typename"] + "\"");
            sb.Append(",\"remark\":\"" + dt.Rows[i]["remark"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //修改
    public void edit(HttpContext context)
    {
        string lx = PageBase.ext_string(context.Request.Params["lx"]);
        string status = "";
        if (lx == "save")
            status = "0";
        else if (lx == "submit")
            status = "1";

        string msg = "";
        string sqlsave = "";
        bool flag;
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }

        string rebh = Utility.Trim(list.LoadData_type[0].REBH);
        string creater = Utility.Trim(list.LoadData_type[0].CREATER);
        string ysdbh = Utility.Trim(list.LoadData_type[0].YSDBH);
        string orbh = Utility.Trim(list.LoadData_type[0].DJBH);
        string name = Utility.Trim(list.LoadData_type[0].NAME);
        string kh = Utility.Trim(list.LoadData_type[0].KH);
        string sl = Utility.Trim(list.LoadData_type[0].SL);
        string remark = Utility.Trim(list.LoadData_type[0].REMARK);
        string pic = Utility.Trim(list.LoadData_type[0].PIC);

        sqlsave = @"update [t_rework_main] set 
                                   [ysdh]='{0}'
                                  ,[orderbh]='{1}'
                                  ,[kh]='{2}'
                                  ,[name]='{3}'
                                  ,[sl]='{4}'
                                  ,[remark]='{5}'
                                  ,[modifier]='{6}'
                                  ,[updatetime]='{7}'
                                  ,[status]='{8}'
                                   where rebh='{9}';";
        sqlsave = string.Format(sqlsave, ysdbh, orbh, kh, name, sl, remark,creater, DateTime.Now.ToString(),status, rebh);

        //图片，先去除最后一个符号
        string[] pic_url = { };
        if (pic != "")
        {
            sqlsave += @"delete from  t_rework_img where rebh='{0}';";
            sqlsave = string.Format(sqlsave, rebh);
            pic_url = pic.TrimEnd('|').Split('|');
            foreach (string p in pic_url)
            {
                if (p != "" && p != null)
                {
                    sqlsave += @"insert into t_rework_img(rebh, picurl) values('{0}','{1}');";
                    sqlsave = string.Format(sqlsave, rebh, p);
                }
            }
        }


        //插入数据库条数
        int zxcount = 0;
        try
        {
            zxcount = DBHelper.ExecuteNonQuery(sqlsave);
        }
        catch (Exception e)
        {
            flag = false;
            msg = e.Message;
        }
        //计算前台传过来的数据条数
        int zqts = 0;
        if (pic == "")
        {
            zqts = list.LoadData_type.Count;
        }
        else
        {
            zqts = list.LoadData_type.Count + pic_url.Length*2;
        }
        if (zxcount == zqts)
        {
            flag = true;
            msg = "修改成功";
        }
        else
        {
            flag = false;
            msg = "修改失败，请联系管理员";
        }

        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //新增
    public void add(HttpContext context)
    {
        string lx = PageBase.ext_string(context.Request.Params["lx"]);
        string status = "";
        if (lx == "save")
            status = "0";
        else if (lx == "submit")
            status = "1";
        string msg = "";
        string djbh = "";
        string sqlsave = "";
        bool flag=false;
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        string creater = Utility.Trim(list.LoadData_type[0].CREATER);
        string username = Utility.Trim(list.LoadData_type[0].USERNAME);
        string ysdbh = Utility.Trim(list.LoadData_type[0].YSDBH);
        string orbh = Utility.Trim(list.LoadData_type[0].DJBH);
        string name = Utility.Trim(list.LoadData_type[0].NAME);
        string kh = Utility.Trim(list.LoadData_type[0].KH);
        string sl = Utility.Trim(list.LoadData_type[0].SL);
        string remark = Utility.Trim(list.LoadData_type[0].REMARK);
        string pic = Utility.Trim(list.LoadData_type[0].PIC);


        if (ysdbh != "")
        {
            //生成单号
            string sqldjbh = @"declare @BH INT;declare @MID varchar(50);SELECT @BH=ISNULL(MAX(RIGHT(rebh,4)),0)+1 FROM 
t_rework_main WHERE LEFT(rebh,2)='RE' AND  RIGHT(LEFT(rebh,8),6)=CONVERT(VARCHAR(10),GETDATE(),12);
set @MID='RE'+CONVERT(VARCHAR(10),GETDATE(),12)+CAST(right((10000+@BH),4) AS VARCHAR(20)) ;
SELECT @MID";
            DataTable dtResult = DBHelper.ExecuteDataTable(sqldjbh);
            djbh = dtResult.Rows[0][0].ToString();
            sqlsave = @"INSERT INTO [t_rework_main]
                                   ([rebh]
                                   ,[ysdh]
                                   ,[orderbh]
                                   ,[kh]
                                   ,[name]
                                   ,[sl]
                                   ,[remark]
                                   ,[status]
                                   ,[creater]
                                   ,[createtime]
                                   ,[modifier]
                                   ,[updatetime]
                                   ,[isactive]
                                   )
                             VALUES
                                   ('{0}'
                                   ,'{1}'
                                   ,'{2}'
                                   ,'{3}'
                                   ,'{4}'
                                   ,'{5}'
                                   ,'{6}'
                                   ,{7}
                                   ,{8}
                                   ,'{9}'
                                   ,{10}
                                   ,'{11}'
                                   ,'{12}'
                                  );";
            sqlsave = string.Format(sqlsave, djbh, ysdbh, orbh, kh, name, sl,remark,status,creater, DateTime.Now.ToString(),creater,DateTime.Now.ToString(),"Y");

            //图片，先去除最后一个符号
            string[] pic_url = { };
            if (pic != "")
            {
                pic_url = pic.TrimEnd('|').Split('|');
                foreach (string p in pic_url)
                {
                    if (p != "" && p != null)
                    {
                        sqlsave += @"insert into t_rework_img(rebh, picurl) values('{0}','{1}');";
                        sqlsave = string.Format(sqlsave, djbh, p);
                    }
                }
            }
            //插入数据库条数
            int zxcount = 0;
            try
            {
                zxcount = DBHelper.ExecuteNonQuery(sqlsave);
            }
            catch (Exception e)
            {
                flag = false;
                msg = e.Message;
            }
            //计算前台传过来的数据条数
            int zqts = 0;
            if (pic == "")
            {
                zqts = list.LoadData_type.Count;
            }
            else
            {
                zqts = list.LoadData_type.Count + pic_url.Length;
            }
            if (zxcount == zqts)
            {
                if (lx == "save")
                {
                    flag = true;
                    msg = "保存成功";
                    string sql_zt = @"insert into t_rework_czlog(rebh,cz,czrid,czrname,cztime,remark) values('{0}','{1}','{2}','{3}','{4}','{5}');";
                    sql_zt = string.Format(sql_zt,djbh,"保存",creater,username,DateTime.Now.ToString(),"");
                    DBHelper.ExecuteNonQuery(sql_zt);
                }
                else
                {
                    flag = true;
                    msg = "提交成功";
                    string sql_zt = @"insert into t_rework_czlog(rebh,cz,czrid,czrname,cztime,remark) values('{0}','{1}','{2}','{3}','{4}','{5}');";
                    sql_zt = string.Format(sql_zt,djbh,"提交",creater,username,DateTime.Now.ToString(),"");
                    DBHelper.ExecuteNonQuery(sql_zt);
                }

            }
            else
            {
                if (lx == "save")
                {
                    flag = false;
                    msg = "保存失败，请联系管理员";
                }
                else
                {
                    flag = false;
                    msg = "提交失败，请联系管理员";
                }
            }
        }


        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    public class GetDate
    {
        public List<typemanager> LoadData_type { get; set; }
    }
    public class typemanager
    {
        public string id;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        public string creater;
        public string CREATER
        {
            get { return creater; }
            set { creater = value; }
        }
        public string username;
        public string USERNAME
        {
            get { return username; }
            set { username = value; }
        }
        public string rebh;
        public string REBH
        {
            get { return rebh; }
            set { rebh = value; }
        }
        public string ysdbh;
        public string YSDBH
        {
            get { return ysdbh; }
            set { ysdbh = value; }
        }

        public string djbh;
        public string DJBH
        {
            get { return djbh; }
            set { djbh = value; }
        }
        public string name;
        public string NAME
        {
            get { return name; }
            set { name = value; }
        }
        public string kh;
        public string KH
        {
            get { return kh; }
            set { kh = value; }
        }
        public string sl;
        public string SL
        {
            get { return sl; }
            set { sl = value; }
        }
        public string remark;
        public string REMARK
        {
            get { return remark; }
            set { remark = value; }
        }
        public string pic;
        public string PIC
        {
            get { return pic; }
            set { pic = value; }
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}